home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 52 < prev    next >
Internet Message Format  |  1996-08-06  |  3KB

  1. Path: informix.com!news
  2. From: Daniel Wood <dwood@informix.com>
  3. Newsgroups: comp.std.c
  4. Subject: Re: Undefined result vs. int's holding undefined values.
  5. Date: 8 Jan 1996 22:00:32 GMT
  6. Organization: Informix Software, Inc. Menlo Park, CA 94025
  7. Message-ID: <4cs460$d6e@news.informix.com>
  8. References: <4ck70b$rd7@news.informix.com> <4ckms5$rd7@news.informix.com> <4cmg0s$1mb@der.twinsun.com> <oZA8wQ9ytpjN084yn@csn.net>
  9. NNTP-Posting-Host: dwood.informix.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; SunOS 5.4 sun4m)
  14. X-URL: news:oZA8wQ9ytpjN084yn@csn.net
  15.  
  16. thads@csn.net (Thad Smith) wrote:
  17. >In article <4cmg0s$1mb@der.twinsun.com>,
  18. >eggert@twinsun.com (Paul Eggert) wrote:
  19. >>This reminds me of a similar bug I found a long time ago when porting
  20. >>the Modula-3 runtime, which contained code that acted something like this:
  21. >>
  22. >>    int sum_overflow (int x, int y) {
  23. >>        return (x + y < x) != (y < 0);
  24. >>    }
  25. >>
  26. >>The C Standard does not guarantee that the above function works,
  27. >>since integer overflow leads to undefined behavior,
  28. >>but when I found that the function did not work with whatever old version
  29. >>of GCC I was using at the time, I reported it as a bug to the GCC maintainers
  30. >>and got a fix from them in a few days.
  31. >>
  32. >>Regardless of what the C Standard says, it should be obvious that it's
  33. >>crucial to have integer overflow checking working properly in an
  34. >>application that needs it.  
  35. >
  36. >I agree, but it is possible to rewrite the function so that it doesn't
  37. >invoke undefined behavior:
  38. >
  39. >  #include <limits.h>
  40. >  int sum_overflow (int x, int y) {
  41. >      return x > 0? (y > INT_MAX - x) : (y < INT_MIN - x);
  42. >  }
  43. >
  44. >Thad
  45.  
  46. I totally understand what you are doing in the above but this would have to
  47. be the ultimate in a cheap out for a vendor.  SCO could claim that before
  48. ever looking at a test case containing a suspected compiler bug that every
  49. arithmetic calculation would have to first have a test similar to the above
  50. to protect against overflow/underflow.  Does an appropriate "SAFE TEST" exist
  51. for multiple.  Has anyone actually seen a real production program where every
  52. calculation was protected against overflow/underflow.
  53.  
  54. Shame on SCO for using such a cheap out.  There is no reason on an intel based
  55. platform not to be able to create an "IMPLEMENTATION DEFINED and consistant"
  56. behavior implementation instead of undefined behavior.  Granted the standard
  57. doesn't require it but I have never seen a program with the kind of extra
  58. checking that seems to be required.  Integer overflow/wraparound producing some
  59. specific defined behavior is easily "doable" on all machine architectures I
  60. know of even if the results might differ on different machines.
  61.  
  62. Do any machines exist which actually explode when you add two number together
  63. such that the result would exceed MAXINT? :-)  Get pratical!
  64.  
  65. I am particularly interested in the answer to my "safe multiply" question
  66. above.  It would be quite funny to find that there is actually no way to
  67. create, in a practical way, a safe c program that used multiple if the
  68. standard was followed to the letter of the law.  I have thought of a way
  69. but it would be alot more involved then the sum_overflow() check above.
  70.  
  71. -- 
  72. If you want a fancy saying then go find yourself a poet.
  73. If you want a bug cracked then you've come to the right place.
  74.  
  75. "The numbers speak to me" - 44 61 6E 20 57 6F 6F 64
  76.  
  77.